import JSZip from 'jszip'

// export default function (zip: JSZip) {
//     zip.generateAsync({ type: "blob" }, function updateCallback(metadata) {
//         var msg = "progression : " + metadata.percent.toFixed(2) + " %";
//         if (metadata.currentFile) {
//             msg += ", current file = " + metadata.currentFile;
//         }
//     })
//         .then(function callback(blob) {

//             // see FileSaver.js
//             saveAs(blob, "example.zip");

//             showMessage("done !");
//         }, function (e) {
//             showError(e);
//         });
// }




// function saveAs(blob: Blob, arg1: string) {
//     throw new Error('Function not implemented.');
// }


export async function downloadtext(type: 'data' | 'url' = 'data', name = 'a.in', data: { name: string, text: string }[]) {
    const zip = new JSZip()

    const ele = document.createElement('a');
    ele.style.display = "none";
    ele.target = "_blank"
    if (type === 'data') {
        for(const v of data) {
            zip.file(v.name, v.text)
        }
    } else {
        let myHeaders = new Headers({
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'text/plain'
        });
        for(const v of data) {
            const z = await fetch(v.text, {
                headers: myHeaders
            })
            const b = await z.blob()
            zip.file(v.name, b)
        }
    }
    document.body.appendChild(ele);
    const blob = await zip.generateAsync({type:"blob"})
    ele.href = URL.createObjectURL(blob);
    ele.download = name;
    ele.click()
    document.body.removeChild(ele);
}
